home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / editors / pur_c_vi.zoo / misccmds.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-11  |  6.8 KB  |  353 lines

  1. /*
  2.  * STevie - ST editor for VI enthusiasts.    ...Tim Thompson...twitch!tjt...
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <ctype.h>
  7. #include "stevie.h"
  8.  
  9. /*
  10.  * opencmd
  11.  *
  12.  * Add a blank line below the current line.
  13.  */
  14.  
  15. opencmd()
  16. {
  17.     /* get to the end of the current line */ 
  18.     while ( Curschar<Fileend && (*Curschar) != '\n' )
  19.         Curschar++;
  20.     /* Try to handle a file that doesn't end with a newline */
  21.     if ( Curschar >= Fileend )
  22.         Curschar = Fileend-1;
  23.     /* Add the blank line */
  24.     appchar('\n');
  25. }
  26.  
  27. issepchar(c)
  28. char c;
  29. {
  30.     if ( strchr(WORDSEP,c) != NULL )
  31.         return(1);
  32.     return(0);
  33. }
  34.  
  35. cntlines(pbegin,pend)
  36. char *pbegin, *pend;
  37. {
  38.     int lnum = 1;
  39.     char *p;
  40.  
  41.     for ( p=pbegin; p<pend; ) {
  42.         if ( *p++ == '\n' )
  43.             lnum++;
  44.     }
  45.     return(lnum);
  46. }
  47.  
  48. fileinfo()
  49. {
  50.     char buff[128];
  51.  
  52.     sprintf(buff,"\"%s\"%s line %d of %d",
  53.         Filename,
  54.         Changed?" [Modified]":"",
  55.         cntlines(Filemem,Curschar),
  56.         cntlines(Filemem,Fileend)-1);
  57.     message(buff);
  58. }
  59.  
  60. gotoline(n)
  61. int n;
  62. {
  63.     char *p;
  64.  
  65.     if ( n == 0 ) {
  66.         if ( (p=prevline(Fileend)) != NULL )
  67.             Curschar = p;
  68.     }
  69.     else {
  70.         /* Start at the top of the file and go down 'n'-1 lines */
  71.         Curschar = Filemem;
  72.         while ( --n > 0 ) {
  73.             if ( (p=nextline(Curschar)) == NULL )
  74.                 break;
  75.             Curschar = p;
  76.         }
  77.     }
  78.     Topchar = Curschar;
  79.     for ( n=0; n<Rows/2; n++ ) {
  80.         if ( (p=prevline(Topchar)) == NULL )
  81.             break;
  82.         Topchar = p;
  83.     }
  84.     updatescreen();
  85. }
  86.  
  87. char *Savedline = NULL;
  88. int Savednum = 0;
  89.  
  90. /*
  91.  * yankline
  92.  *
  93.  * Save a copy of the current line(s) for later 'p'lacing.
  94.  */
  95.  
  96. yankline(n)
  97. {
  98.     char *savep, *p, *q;
  99.     int leng, k;
  100.  
  101.     if ( Savedline != NULL )
  102.         free(Savedline);
  103.     savep = Curschar;
  104.     /* go to the beginning of the current line. */
  105.     beginline();
  106.     /* compute length of line */
  107.     for ( p=Curschar,leng=0; ; p++ ) {
  108.         if ( *p == '\n' ) {
  109.             /* keep going until we've seen 'n' lines */
  110.             if ( --n <= 0 )
  111.                 break;
  112.         }
  113.         leng++;
  114.     }
  115.     /* save a copy of it */
  116.     Savedline = malloc((unsigned)(leng+2));
  117.     for ( p=Curschar,q=Savedline,k=0; k<leng; k++ )
  118.         *q++ = *p++;
  119.     /* get the final newline */
  120.     *q++ = *p;
  121.     *q = '\0';
  122.     Curschar = savep;
  123.     Savednum = leng+1;
  124. }
  125.  
  126. /*
  127.  * putline
  128.  *
  129.  * If there is a currently saved line(s), 'p'ut it.
  130.  * If k==1, 'P'ut the line (i.e. above instead of below.
  131.  */
  132.  
  133. putline(k)
  134. int k;
  135. {
  136.     char *p;
  137.     int n;
  138.  
  139.     if ( Savedline == NULL )
  140.         return;
  141.     message("Inserting saved stuff...");
  142.     if ( k == 0 ) {
  143.         /* get to the end of the current line */
  144.         while ( Curschar<Fileend && *Curschar != '\n' )
  145.             Curschar++;
  146.     }
  147.     else
  148.         beginline();
  149.     /* append or insert the characters of the saved line */
  150.     for ( p=Savedline,n=0; n<Savednum; p++,n++ ) {
  151.         if ( k == 0 )
  152.             appchar(*p);
  153.         else
  154.             inschar(*p);
  155.     }
  156.     /* We want to end up at the beginning of the line. */
  157.     while ( n-->1 )
  158.         Curschar--;
  159.     if ( k == 1 )
  160.         Curschar--;
  161.     beginline();
  162.     message("");
  163.     updatescreen();
  164. }
  165.  
  166. inschar(c)
  167. int c;
  168. {
  169.     register char *p;
  170.  
  171.     /* Move everything in the file over to make */
  172.     /* room for the new char. */
  173.     if ( ! canincrease(1) )
  174.         return;
  175.  
  176.     for ( p=Fileend; p>Curschar; p-- ) {
  177.         *p = *(p-1);
  178.     }
  179.     *Curschar++ = c;
  180.     Fileend++;
  181.     CHANGED;
  182. }
  183.  
  184. insstr(s)
  185. char *s;
  186. {
  187.     register char *p;
  188.     int k, n = strlen(s);
  189.  
  190.     /* Move everything in the file over to make */
  191.     /* room for the new string. */
  192.     if ( ! canincrease(n) )
  193.         return;
  194.  
  195.     for ( p=Fileend-1+n; p>Curschar; p-- ) {
  196.         *p = *(p-n);
  197.     }
  198.     for ( k=0; k<n; k++ )
  199.         *Curschar++ = *s++;
  200.     Fileend += n;
  201.     CHANGED;
  202. }
  203.  
  204. appchar(c)
  205. int c;
  206. {
  207.     char *p, *endp;
  208.  
  209.     /* Move everything in the file over to make */
  210.     /* room for the new char. */
  211.     if ( ! canincrease(1) )
  212.         return;
  213.  
  214.     endp = Curschar+1;
  215.     for ( p=Fileend; p>endp; p-- ) {
  216.         *p = *(p-1);
  217.     }
  218.     *(++Curschar) = c;
  219.     Fileend++;
  220.     CHANGED;
  221. }
  222.  
  223. canincrease(n)
  224. int n;
  225. {
  226.     if ( (Fileend+n) >= Filemax ) {
  227.         message("Can't add anything, file is too big!");
  228.         State = NORMAL;
  229.         return(0);
  230.     }
  231.     return(1);
  232. }
  233.  
  234. #define NULL 0
  235.  
  236. delchar()
  237. {
  238.     char *p;
  239.  
  240.     /* Check for degenerate case; there's nothing in the file. */
  241.     if ( Filemem == Fileend )
  242.         return;
  243.     /* Delete the character at Curschar by shifting everything */
  244.     /* in the file down. */
  245.     for ( p=Curschar+1; p<Fileend; p++ )
  246.         *(p-1) = *p;
  247.     /* If we just took off the last character of a non-blank line, */
  248.     /* we don't want to end up positioned at the newline. */
  249.     if ( *Curschar=='\n' && Curschar>Filemem && *(Curschar-1)!='\n' )
  250.         Curschar--;
  251.     Fileend--;
  252.     CHANGED;
  253. }
  254.  
  255. delword(deltrailing)
  256. int deltrailing;    /* 1 if trailing white space should be removed. */
  257. {
  258.     int c = *Curschar;
  259.     char *p = Undobuff;
  260.  
  261.     /* The Undo string is an 'i'nsert of the word we're deleting. */
  262.     *p++ = 'i';
  263.     /* If we're positioned on a word separator... */
  264.     if ( issepchar(c) && ! isspace(c) ) {
  265.         /* If we're on a non-space separator, remove */
  266.         /* the separators and any following space. */
  267.         while ( issepchar(c) && ! isspace(c) ) {
  268.             /* Add the deleted character to the Undobuff */
  269.             *p++ = *Curschar;
  270.             delchar();
  271.             c = *Curschar;
  272.         }
  273.     }
  274.     else {    /* we're positioned in the middle of a word */
  275.         int endofline = 0;
  276.         while ( ! issepchar(*Curschar) && *Curschar != '\n' ) {
  277.             /* If the next char is a newline, we note */
  278.             /* that fact here, because delchar() won't */
  279.             /* position us there afterword. */
  280.             if ( *(Curschar+1) == '\n' )
  281.                 endofline = 1;
  282.             /* Add the deleted character to the Undobuff */
  283.             *p++ = *Curschar;
  284.             delchar();
  285.             if ( endofline )
  286.                 break;
  287.         }
  288.     }
  289.     if ( deltrailing ) {
  290.         /* remove any trailing white space */
  291.         while ( isspace(*Curschar) && *Curschar != '\n' ) {
  292.             /* Add the deleted character to the Undobuff */
  293.             *p++ = *Curschar;
  294.             delchar();
  295.         }
  296.     }
  297.     *p++ = '\033';
  298.     *p = '\0';
  299. }
  300.  
  301. delline(nlines)
  302. {
  303.     int nchars;
  304.     char *p, *q;
  305.  
  306.     /* If we're not at the beginning of the line, get there. */
  307.     if ( *Curschar != '\n' ) {
  308.         /* back up to the previous newline (or the beginning */
  309.         /* of the file. */
  310.         while ( Curschar > Filemem ) {
  311.             if ( *Curschar == '\n' ) {
  312.                 Curschar++;
  313.                 break;
  314.             }
  315.             Curschar--;
  316.         }
  317.     }
  318.     message("Deleting...");
  319.     while ( nlines-- > 0 ) {
  320.         /* Count the characters in the line */
  321.         for ( nchars=1,p=Curschar; p<Fileend&&*p!='\n'; p++,nchars++ )
  322.             ;
  323.         /* Delete the characters of the line */
  324.         /* by moving everything else in the file down. */
  325.         q = Curschar;
  326.         p = Curschar+nchars;
  327.         while ( p<Fileend )
  328.             *q++ = *p++;
  329.         Fileend -= nchars;
  330.         CHANGED;
  331.  
  332.         /* If we delete the last line in the file, back up */
  333.         if ( Curschar >= Fileend ) {
  334.             if ( (Curschar=prevline(Curschar)) == NULL )
  335.                 Curschar = Filemem;
  336.             /* and don't try to delete any more lines */
  337.             break;
  338.         }
  339.     }
  340.     message("");
  341. }
  342.  
  343. char *strchr(s,c)
  344. char *s;
  345. int c;
  346. {
  347.     do {
  348.         if ( *s == c )
  349.             return(s);
  350.     } while (*s++);
  351.     return(NULL);
  352. }
  353.